package com.itheima01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/*
    查询db2数据库中product表的所有数据
*/
public class JDBCDemo01 {
    public static void main(String[] args)throws Exception {
    // 注册驱动
        Class.forName("com.mysql.jdbc.Driver");

    // 获取连接
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2", "root", "123");

    // 获取执行对象
        Statement stat = con.createStatement();

    // 执行sql语句，并返回结果
        String sql = "select * from product";
        ResultSet rs = stat.executeQuery(sql);

    // 处理结果
        while (rs.next()){
            System.out.println(rs.getInt("id")+"\t"+rs.getString("name")+"\t\t"+rs.getDouble("price"));
        }

    // 释放资源
        rs.close();
        stat.close();
        con.close();

    }
}
